﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using CoreInmobiliario.Data;

namespace CoreInmobiliario.Business
{
    [DataObject]
    public class Empresa
    {
        public static Empresa GetInstance()
        {
            return new Empresa();
        }

        private static DBDataContext GetDBDataContext()
        {
            Factories.DBDataContextFactory factory = new Factories.DBDataContextFactory();
            return factory.CreateDataContext() as DBDataContext;
        }

        public Data.Empresa GetById(int empresaId)
        {
            Data.Empresa empresa = null;

            using (DBDataContext dataContext = GetDBDataContext())
            {
                try
                {
                    empresa = dataContext.Empresas.FirstOrDefault(e => e.EmpresaId.Equals(empresaId));
                }
                catch (Exception ex)
                {

                }
            }
            return empresa;
        }

        public Boolean UpdateEmpresa(Data.Empresa e)
        {
            using (DBDataContext dc = GetDBDataContext())
            {
                try
                {
                    Data.Empresa empresa = dc.Empresas.FirstOrDefault(emp => emp.EmpresaId.Equals(e.EmpresaId));
                    empresa.Descripcion = e.Descripcion;

                    dc.SubmitChanges();
                    return true;
                }
                catch
                {
                    return false;
                }
            }
        }
    }
}
